import 'dart:io';

import 'package:aihu_getx_app/app/modules/commons/commons.dart';
import 'package:aihu_getx_app/config/app_init_data.dart';
import 'package:aihu_getx_app/theme/theme.dart';
import 'package:aihu_getx_app/utils/cache_keys.dart';
import 'package:aihu_getx_app/utils/constants.dart';
import 'package:aihu_getx_app/utils/init_system_data_util.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sp_util/sp_util.dart';
import 'package:window_manager/window_manager.dart';

class AppContext {
  static bool isDesktop =
      Platform.isWindows || Platform.isLinux || Platform.isMacOS;

  static Future<AppInitData> init() async {
    WidgetsFlutterBinding.ensureInitialized();
    if (isDesktop) {
      await windowManager.ensureInitialized();
      WindowOptions windowOptions = const WindowOptions(
        size: Size(800, 600),
        center: true,
        backgroundColor: Colors.transparent,
        skipTaskbar: false,
        titleBarStyle: TitleBarStyle.normal,
        title:
            "${Constants.appName} - ${Constants.appVersion} - ${Constants.appDescription}",
      );
      windowManager.waitUntilReadyToShow(windowOptions, () async {
        await windowManager.show();
        await windowManager.focus();
      });
    }
    await SystemChrome.setEnabledSystemUIMode(
      SystemUiMode.manual,
      overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top],
    );
    SystemChrome.setSystemUIOverlayStyle(
        const SystemUiOverlayStyle(statusBarColor: Colors.transparent));
    await SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
    ]); // 禁止横屏
    LogUtil.init(isDebug: kDebugMode);
    await SpUtil.getInstance();
    AppInitData initData = AppInitData();
    try {
      String page = Commons.getLastSplashPage();
      initData.initialRoute = page;
    } catch (e) {}
    var currentThemeName = SpUtil.getString(CacheKeys.currentThemeName,
        defValue: AppTheme.defaultThemeName);
    var currentTheme = AppTheme.themeList.firstWhere(
        (item) => item.name == currentThemeName,
        orElse: () => AppTheme.themeList[0]);
    initData.theme = currentTheme.data;
    configLoading();
    InitSystemDataUtil.initDictData();
    await ScreenUtil.ensureScreenSize();
    return Future.value(initData);
  }

  static void configLoading() {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.dark
      ..indicatorSize = 45.0
      ..radius = 10.0
      ..progressColor = Colors.yellow
      ..backgroundColor = Colors.green
      ..indicatorColor = Colors.yellow
      ..textColor = Colors.yellow
      ..maskColor = Colors.blue.withOpacity(0.5)
      ..userInteractions = true
      ..dismissOnTap = false;
    // ..customAnimation = CustomAnimation();
  }
}
